const mongoose = require("mongoose");
// 创建Category集合的数据规则
const schema = new mongoose.Schema({
  name: { type: String },
  // ref 指定关联的 模型
  parent: { type: mongoose.SchemaTypes.ObjectId, ref: "Category" },
});

schema.virtual("children", {
  localField: "_id",
  foreignField: "parent",
  justOne: false,
  ref: "Category",
});

schema.virtual("newsList", {
  localField: "_id",
  foreignField: "categories",
  justOne: false,
  ref: "Article",
});

// 操作 Categorys 集合的 model
module.exports = mongoose.model("Category", schema);
